package cn.liufuwen.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.dubbo.config.annotation.Service;

import cn.liufuwen.domain.Goods;
import cn.liufuwen.domain.Orders;
import cn.liufuwen.domain.OrdersDetail;
import cn.liufuwen.entity.OrdersDetailDTO;
import cn.liufuwen.mapper.GoodsMapper;
import cn.liufuwen.mapper.OrdersDetailMapper;
import cn.liufuwen.mapper.OrdersMapper;
import cn.liufuwen.service.OrdersDetailService;
import lombok.extern.java.Log;
@Log
//注册为 Dubbo 服务
@Service(version = "${demo.service.version}") 
public class OrdersDetailServiceImpl implements OrdersDetailService {
	@Autowired
    private OrdersMapper ordersMapper;
	@Autowired
	private OrdersDetailMapper ordersDetailMapper;
	@Autowired
    private GoodsMapper goodsMapper;
	
	@Override
	public List<OrdersDetailDTO> listOrdersDetail(int userID) {
		List<OrdersDetail> listOrdersDetail= ordersDetailMapper.selectByUserID(userID);
		List<OrdersDetailDTO> listOrdersDetailDTO = new ArrayList<OrdersDetailDTO>();
		for(OrdersDetail od:listOrdersDetail){
			OrdersDetailDTO ordersDetailDTO = new OrdersDetailDTO();
			ordersDetailDTO.setId(od.getId());
			ordersDetailDTO.setGoodsId(od.getGoodsId());
			ordersDetailDTO.setOrderId(od.getOrderId());
			ordersDetailDTO.setPrice(od.getPrice());
			ordersDetailDTO.setNumber(od.getNumber());
			listOrdersDetailDTO.add(ordersDetailDTO);
		}
		return listOrdersDetailDTO;
	}

	@Override
	public List<OrdersDetailDTO> ordersDetailByID(int orderID) {
		List<OrdersDetailDTO> listOrdersDetailDTO = new ArrayList<OrdersDetailDTO>();
		List<OrdersDetail> listOrdersDetail=ordersDetailMapper.selectByOrderID(orderID);
		for(OrdersDetail od:listOrdersDetail){
			OrdersDetailDTO ordersDetailDTO = new OrdersDetailDTO();
			ordersDetailDTO.setId(od.getId());
			ordersDetailDTO.setGoodsId(od.getGoodsId());
			ordersDetailDTO.setOrderId(od.getOrderId());
			ordersDetailDTO.setPrice(od.getPrice());
			ordersDetailDTO.setNumber(od.getNumber());
			listOrdersDetailDTO.add(ordersDetailDTO);
		}
		return listOrdersDetailDTO;
	}

	@Override
	public int addGoods(OrdersDetailDTO ordersDetailDTO) {
		Date dDate = new Date();
		int orderId = 0;
		int i=0;
		// 订单不存在时创建订单
		if(ordersMapper.selectIdByUserID(ordersDetailDTO.getUserId()) == null){ 
			Orders orders = new Orders();
			orders.setUserId(ordersDetailDTO.getUserId());
			orders.setCreateTime(dDate);
			ordersMapper.insertSelective(orders);
			log.info("创建订单成功"+orders.toString());
		}
		//获取订单号
		orderId = Integer.parseInt(ordersMapper.selectIdByUserID(ordersDetailDTO.getUserId()));
		if(orderId>0){
			//先进行出库
			Goods record = new Goods();
			record.setId(ordersDetailDTO.getGoodsId());
			record.setGoodsStock(ordersDetailDTO.getNumber());
			i= goodsMapper.updateReStockById(record);
		}
		if(i>0){
			//创建订单
			OrdersDetail ordersDetail = new OrdersDetail();
			ordersDetail.setGoodsId(ordersDetailDTO.getGoodsId());
			ordersDetail.setNumber(ordersDetailDTO.getNumber());
			ordersDetail.setOrderId(orderId);
			ordersDetail.setPrice(ordersDetailDTO.getPrice());
			i = ordersDetailMapper.insertSelective(ordersDetail);
		}
		
		return i;
	}
	
	@Override
	public int delGoods(OrdersDetailDTO ordersDetailDTO) {
		int i =0;
		int id = ordersDetailDTO.getId();
		OrdersDetail ordersDetail = ordersDetailMapper.selectByPrimaryKey(id);
		//移除订单详情
		i = ordersDetailMapper.deleteByPrimaryKey(id);
		//更新商品信息
		Goods goods = new Goods();
		goods.setId(ordersDetail.getGoodsId());
		goods.setGoodsStock(ordersDetail.getNumber());
		i = goodsMapper.updateAdStockById(goods);
		return i;
	}
}
